<?php

declare(strict_types=1);

namespace app\common\model;

use think\Exception;
use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class UserMoneyLog extends BaseModel
{
    //
    //开通会员处理业务逻辑
    public static function vipayAfter($uid, $money, $recharge_time,$expiration_time,$user_type,$desc,$order_sn)
    {
        $user = User::where('id', $uid)->lock(true)->find();
        if (!$user) {
            throw new Exception("用户没找到");
        }
        $user->user_type = $user_type;
        $user->recharge_time = $recharge_time;
        $user->expiration_time = $expiration_time;
        $user->save();
        UserMemberLog::create(['user_id' => $uid, 'money' =>  $money, 'recharge_time' => $recharge_time, 'expiration_time' => $expiration_time, 'memo' => $desc,'order_sn'=>$order_sn]);
        return true;
    }

     //
    //开通会员处理业务逻辑
    public static function aliVipayAfter($uid, $money, $recharge_time,$expiration_time,$user_type,$desc,$order_sn)
    {
        $user = User::where('id', $uid)->lock(true)->find();
        if (!$user) {
            throw new Exception("用户没找到");
        }
        $user->user_type = $user_type;
        $user->recharge_time = $recharge_time;
        $user->expiration_time = $expiration_time;
        $user->save();
        UserMemberLog::create(['user_id' => $uid, 'money' =>  $money, 'recharge_time' => $recharge_time, 'expiration_time' => $expiration_time, 'memo' => $desc,'order_sn'=>$order_sn]);
        return "success";
    }
       

    //支付之后
    public static function payAfter($uid, $money, $memo,$type,$order_sn)
    {
        $user = User::where('id', $uid)->lock(true)->find();
        if (!$user) {
            throw new Exception("用户没找到");
        }
        $before = $user->money;
        if($type == PublicStatus::USER_MONEY_STATUS_EXPEND){ //支出
            if($user->money < $money){
                throw new Exception("余额不足");
            }
            $user->money -= $money;
        }else{
            $user->money += $money; //收入
        }
        $user->save();
        $after = $user->money;
        $memo = $memo;
        Db::name('user_money_log')->save(['user_id' => $uid, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo,'type'=>$type,'order_sn'=>$order_sn]);
        return true;
    }


      //支付之后
      public static function aliPayAfter($uid, $money, $memo,$type,$order_sn)
      {
          $user = User::where('id', $uid)->lock(true)->find();
          if (!$user) {
              throw new Exception("用户没找到");
          }
          $before = $user->money;
          if($type == PublicStatus::USER_MONEY_STATUS_EXPEND){ //支出
              if($user->money < $money){
                  throw new Exception("余额不足");
              }
              $user->money -= $money;
          }else{
              $user->money += $money; //收入
          }
          $user->save();
          $after = $user->money;
          $memo = $memo;
          Db::name('user_money_log')->save(['user_id' => $uid, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo,'type'=>$type,'order_sn'=>$order_sn]);
          return "success";
      }
  

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }



    public function order()  {
        return $this->belongsTo(Order::class,);
    }

    //余额退款
    public static function moneyRefund($uid, $money)
    {
        $user = User::where('id', $uid)->lock(true)->find();
        if (!$user) {
            throw new Exception("用户没找到");
        }
        $before = $user->money;
        $user->money += $money;
        $user->save();
        $after = $user->money;
        $memo = '来自退款';
        Db::name('user_money_log')->save(['user_id' => $uid, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
        return true;
    }
}
